<?php
namespace User\Controller;
use Think\Controller;

class EventController extends Controller
{
	public function index()
	{

            $eventId = I('get.id', -1, '');
            $teamName = I('get.teamName', '', '');

            $conditions = array(
                'event_id' => $eventId
            );
            $result = M( 'organiztion' )
                ->where( $conditions )
                ->order( 'score desc, praise desc' )
                ->select();

            //$wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
            //$wx = $wechat->buildWXJSApi();
            //$wx['appid'] = $this->appid;

            //$this->assign( 'wx', $wx );
            $this->assign( 'result', $result );

            $this->assign('eventId', $eventId);
            layout(false);
            $this->display( 'index_new' );
	}

        /*
        *异步加载参赛队伍
        */
        public function ajaxOrgList()
        {
             $eventId = I('get.id', -1, '' );
             $page = I('get.page', 1, '');
             $teamName = I('get.teamName', '');

             $organiztion = D("Organiztion");

             $result = $organiztion->getOrganiztionList($eventId, $page, $teamName, 2);
             echo json_encode($result);

        }

        /*
        *检验评委登录状态
        */
        public function ajaxOnlineCheck()
        {
            $user = session('event_user');

            $orgId   = I('get.orgId', 0, '');
            $eventId = I('get.eventId', 0, '');
            $data['code']  = 302;
            $data['msg']   = '未登录';
            $data['orgId'] = $orgId;
            $data['eventId'] = $eventId;
            //$data['eventId'] = $eventId;
            if($user != null)
            {
                $data['code'] = 200;
                $data['msg']  = '已登录';
            }
            $this->ajaxReturn($data);
        }

        /*
        *评委登录
        *
        */
        public function login()
        {
            $username = I('post.username', '', '');
            $password = I('post.password', '', '');
            $orgId    = I('post.orgId', 0, '');
            $eventId  = I('post.eventId', 0, '');

            $data['orgId']   = $orgId;
            $data['eventId'] = $eventId;
            if( D('EventUser')->login($username, $password) )  //登录成功
            {
                $data['code'] = 200;

            }
            else //登录失败
            {
                $data['code'] = 302;

            }
            $this->ajaxReturn($data);
        }

        /*
        *评分页
        */
        public function detail()
        {
            $orgId   = I('get.orgId', 0, '');
            $eventId = I('get.eventId', 0, '');
            if($orgId == 0 | $eventId == 0)
            {
                $this->error('非法操作');
                return;
            }

            $user = session( 'event_user' );
            if( !$user ){
                $this->error( '未登录' );
                return;
            }

            $conditions = array(
                //'esm_event_item.event_id' => $eventId,
                'esm_organiztion.id' => $orgId
            );

            $columns = array(
                'esm_event_item.id' => 'id',
                'esm_event_item.event_id' => 'event',
                'esm_event_item.name' => 'name',
                'esm_organiztion.id' => 'organiztion',
                'esm_organiztion_item.score' => 'scored'
            );

            $result = M( 'event_item' )
                ->join( 'LEFT JOIN esm_organiztion ON esm_event_item.event_id = esm_organiztion.event_id' )
                ->join( 'LEFT JOIN esm_organiztion_item ON esm_event_item.id = esm_organiztion_item.item_id and esm_organiztion.id = esm_organiztion_item.organiztion_id' )
                ->field( $columns )
                ->where( $conditions )
                //->group( 'esm_event_item.id' )
                ->select();
            //$mode = new \Think\Model();
            //$result = $mode->query( "select * from ( select distinct esm_event_item.id, esm_event_item.event_id, esm_event_item.name from esm_event_item left join esm_organiztion on esm_event_item.event_id = esm_organiztion.event_id where esm_organiztion.event_id =".$eventId." and esm_organiztion.id = ".$orgId.") as item left join esm_organiztion_item on item.organiztion_id = esm_organiztion_item.organiztion_id" );
            //
            $organiztion = M( 'organiztion' )->where( $conditions )->find();
            //dump( $result );
            $this->assign( "eventId", $eventId );
            $this->assign( "orgId", $orgId );
            $this->assign( 'organiztion', $organiztion );
            $this->assign( "result", $result );
            layout(false);
            $this->display();
        }
        /*
        *评委评分
        */
	public function insertScore()
	{
		$itemId        = I('get.id', 0);
		$organiztionId = I('get.orgId', 0);
                $scoreNum      = I('get.score', 0);

                if($scoreNum > 0)
                {
                	$item = D("OrganiztionItem");

                	$result = $item->setScoreItem($itemId, $organiztionId, $scoreNum);

                	$this->ajaxReturn($result);
                }
                else
                {
                	return false;
                }

	}
}
?>
